/*
                        *********************
*************************** C SOURCE FILE ************************************
**                      *********************                               **
**                                                                          **
*/

#define _CLIENT_C_SRC

/****************************************************************************/
/**                                                                        **/
/** MODULES USED                                                           **/
/**                                                                        **/
/****************************************************************************/

#include "client.h"

/****************************************************************************/
/**                                                                        **/
/** DEFINITIONS AND MACROS                                                 **/
/**                                                                        **/
/****************************************************************************/


/****************************************************************************/
/**                                                                        **/
/** TYPEDEFS AND STRUCTURES                                                **/
/**                                                                        **/
/****************************************************************************/


/****************************************************************************/
/**                                                                        **/
/** PROTOTYPES OF LOCAL FUNCTIONS                                          **/
/**                                                                        **/
/****************************************************************************/


/****************************************************************************/
/**                                                                        **/
/** EXPORTED VARIABLES                                                     **/
/**                                                                        **/
/****************************************************************************/


/****************************************************************************/
/**                                                                        **/
/** GLOBAL VARIABLES                                                       **/
/**                                                                        **/
/****************************************************************************/



/****************************************************************************/
/**                                                                        **/
/** EXPORTED FUNCTIONS                                                     **/
/**                                                                        **/
/****************************************************************************/



/****************************************************************************/
/**                                                                        **/
/** LOCAL FUNCTIONS                                                        **/
/**                                                                        **/
/****************************************************************************/
int16s main(int16s argc, int8u *argv[])
{
	int16u	i,j,x=0,y=0;
	int16s 	sockfd;
	int16u 	len,lenge;
	struct 	sockaddr_in address;
	int16s 	result;
	int16u	answer=0,state,n;
	int16s	point[4][2];
	int8u 	*p;
	int16u	buf[4];

	argv++;
	if(argc == 5)
	{
		for(i=0;i<4;i++)
		{
			j=0;
			p=argv[i];
			while(*p)
			{
				if(*p=='-')
				{
					p++;
					if((*p>='0')&&(*p<='9'))
					{
						point[i][j] = -1*(*p-48);
						j++;
						p++;
						if(*p==',')
						{
							p++;
						}
						else if(*p=='\0');
						else
						{
							printf("Error parameter\n");
							return 0;
						}
					}
					else
					{
						printf("Error parameter\n");
						return 0;
					}
				}
				else if((*p>='0')&&(*p<='9'))
				{
					point[i][j] = *p-48;
					j++;
					p++;
					if(*p==',')
					{
						p++;
					}
					else if(*p == '\0');
					else
					{printf("C");
						printf("Error parameter\n");
						return 0;
					}
				}
				else
				{
					printf("Error parameter\n");
						return 0;
				}
			}
		}

		for(i=0;i<3;i++)
		{
			for(j=i;j<3;j++)
			{
				if((point[i][0]==point[j+1][0])&&(point[i][1]==point[j+1][1]))
				{
					printf("It's not square.\n %d\n",i);
					return 0;
				}
				if(point[i][0]==point[j+1][0])
				{
					x++;
				}
				if(point[i][1]==point[j+1][1])
				{
					y++;
				}
			}
		}

		if((x!=2)||(y!=2))
		{
			printf("It's not square.\n");
			return(0);
		}
		else
		{
			printf("It is square.\n");
		}

		printf("%d,%d  %d,%d  %d,%d  %d,%d\n",point[0][0],point[0][1],point[1][0],point[1][1],point[2][0],point[2][1],point[3][0],point[3][1]);

		// Create a socket for the client. 
		sockfd = socket(AF_INET, SOCK_STREAM, 0);
		// Socket name, as agreed with the server.
		address.sin_family = AF_INET;
		address.sin_addr.s_addr = inet_addr("192.168.1.245");
		address.sin_port = 1803;
		len = sizeof(address);

		// Connect our socket to the server's socket.
		result = connect(sockfd, (struct sockaddr *)&address, len);
		if(result == -1)
		{
			perror("OOPS!!: connect function error");
			exit(1);
		}


	state = data;
	n = 0;
	while(state <= recieve )
	{
		switch(state)
		{
			case data:
			while(n<4)
			{
				switch(n)	// write data to server
				{
					case 0:
						write(sockfd, point[0], 2*sizeof(int16s));
						read(sockfd, &answer, sizeof(int16u));
						if(answer)
						{
							n = 1;
						}
						else
						{
							printf("Server not reply data1\n");
						}

						break;
					case 1:
						write(sockfd, point[1], 2*sizeof(int16s));
						read(sockfd, &answer, sizeof(int16u));
						if(answer)
						{
							n = 2;
						}
						else
						{
							printf("Server not reply data2\n");
						}
						break;
					case 2:
						write(sockfd, point[2], 2*sizeof(int16s));
						read(sockfd, &answer, sizeof(int16u));
						if(answer)
						{
							n = 3;
						}
						else
						{
							printf("Server not reply data3\n");
						}
						break;
					case 3:
						write(sockfd, point[3], 2*sizeof(int16s));
						read(sockfd, &answer, sizeof(int16u));
						if(answer)
						{
							n = 4;
							state = recieve;
						}
						else
						{
							printf("Server not reply data4\n");
						}
						break;
				}
			}
				break;
			case recieve:

				result = read(sockfd,buf, 4*sizeof(int16u));
				if(result)
				{
					answer = 1;
					write(sockfd, &answer, sizeof(int16u));

					printf("Quadrant 1 is %d.\n",buf[0]);
					printf("Quadrant 2 is %d.\n",buf[1]);
					printf("Quadrant 3 is %d.\n",buf[2]);
					printf("Quadrant 4 is %d.\n",buf[3]);
				}
				else
				{
					answer = 0;
					write(sockfd, &answer, sizeof(int16u));
					printf("send back error.\n");
				}

				state = recieve+1;
				
				break;
			
		}
	}


		// Close socket and exit program.
		close(sockfd);
		exit(0);

	}
	else
	{
		printf("Error,the number of argument must be 5\n");
	}
	return(0);
}

/****************************************************************************/
/**                                                                        **/
/** EOF                                                                    **/
/**                                                                        **/
/****************************************************************************/

